package com.stylefeng.guns.modular.channel_manage.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.common.exception.BizExceptionEnum;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.modular.apple.utils.CodeUtil;
import com.stylefeng.guns.modular.apple.vo.Paging;
import com.stylefeng.guns.modular.apple_app_manage.dao.ApplicationMapper;
import com.stylefeng.guns.modular.apple_app_manage.model.Application;
import com.stylefeng.guns.modular.channel_manage.model.ChannelInfo;
import com.stylefeng.guns.modular.channel_manage.dao.ChannelInfoMapper;
import com.stylefeng.guns.modular.channel_manage.service.IChannelInfoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.modular.channel_manage.model.ChannelPromoteDO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 渠道信息表 服务实现类
 * </p>
 *
 * @author GodRui
 * @since 2020-06-26
 */
@Service
public class ChannelInfoServiceImpl extends ServiceImpl<ChannelInfoMapper, ChannelInfo> implements IChannelInfoService {

    @Resource
    private ApplicationMapper appMapper;

    @Override
    @Transactional
    public void addChannel(String channelName) {
        if (StringUtils.isEmpty(channelName)){
            throw new GunsException(BizExceptionEnum.CHANNEL_NAME_IS_NOLL);
        }

        int channelCount = this.selectCount(null);
        String code = CodeUtil.toSerialCode(channelCount);

        ChannelInfo channelInfo = new ChannelInfo();
        channelInfo.setChannelName(channelName);
        channelInfo.setChannelNo(code);
        channelInfo.setCreateTime(new Date());
        channelInfo.setUserId(ShiroKit.getUser().getId());
        insert(channelInfo);
    }

    @Override
    public Object channelPromoteList() {
        //1、登录用户下的应用列表，管理员展示所有应用列表
        EntityWrapper<Application> apWrapper = new EntityWrapper<>();
        if (!ShiroKit.isAdmin()){
            apWrapper.eq("user_id",ShiroKit.getUser().getId());
        }
        List<Application> applicationList = appMapper.selectList(apWrapper);

        //2、商户下的渠道列表
        EntityWrapper<ChannelInfo> wrapper = new EntityWrapper<>();
        wrapper.isNull("delete_time");
        if (!ShiroKit.isAdmin()){
            wrapper.eq("user_id",ShiroKit.getUser().getId());
        }

        List<ChannelInfo> channelInfoList = this.selectList(wrapper);

        //3、使用每个渠道号与每个应用拼接出对应的推广链接，（渠道名称，应用名称，推广链接）
        List<ChannelPromoteDO> promoteDOList = new ArrayList<>();

        channelInfoList.forEach(c->{
            if (applicationList.size() == 0){
                promoteDOList.add(ChannelPromoteDO.builder().channelId(c.getId()).channelName(c.getChannelName()).createTime(c.getCreateTime()).build());
            }else{
                applicationList.forEach(a->{
                    if (a.getUserId().equals(c.getUserId())){
                        promoteDOList.add(ChannelPromoteDO.builder().deleteTime(c.getDeleteTime()).channelId(c.getId()).channelName(c.getChannelName()).createTime(c.getCreateTime()).appName(a.getAppName()).merchantName(a.getUserName()).promoteUrl(a.getAppUrl()+"&channelNo="+c.getChannelNo()).build());
                    }
                });
            }
        });

        Paging<ChannelPromoteDO> paging = new Paging<>();
//        paging.setTotal((long) promoteDOList.size());
        paging.setItems(promoteDOList);

        return paging;
    }
}
